Refactored some parts of the code for readability