More restructuring and code cleanup