Refactored some code, commented, added design doc