some restructuring and refactoring