Refactoring code; adding documentation to the private functions