Refactored the API and tidied up the code.