Restructuring and cleaning code