refactored functions and wrote documentation