Refactored and commented code.