refactored api to make more sense. Refactored code into seperate functions