refactored some code to make it more pythonic