General code restructure