refactored code to readability, testing, docs, maintainability