Added tests, cleanup code