Refactored some code for readability