Clean up key code and add basic migration logic.