refactoring code and adding documentation