Move register code into its own function