refactored code and implemented all todos