Refactored code and wirtten docs