Added comments and refactored functions