Refactored code and made comments clearer