Commented and refactored to clean code