Refactored and moved API functions to a module

Keeps the app.js cleaner.