refactored some code for less complexity