refactored code and cleaned up the structure