Tidied up code, fixed some small API issues.