Reorganise to use an api class rather than a global connection