Some restructuring and refactoring