Restructuring, refactoring and general cleanup