Add tests, cleanup, refactor