Modified: refactored functions, added comments