Refactor most of the API