Added headers to API response, misc refactoring.