Major refactoring of code to simplify API calls