Refactored; added comments