Add tests and cleanup code