Refactor api

This refactoring is not complete. But the basic project structure has been built. Would be good to merge this to master asap.