massive code restructuring and cleanup