restructuring and bug fixes