Iron carbide and nitride via a flexible route: synthesis, structure and magnetic properties