refactored and wrote documentaion