refactored code, updated comments