Finished refactoring into functions and API calls
Updated database connection setup.
Updated to work correctly with new database connection method.