Implementing more of the API and some refactoring