Cleanup code and more tests