Started refactoring the api code