Restructure API handlers, refactor