Clean up and add some tests