Restructuring and code cleanup