Misc code restructuring