refactored some of the code