refactored some code in an attempt to make it easier to understand