Clean up some code, change the API a bit