Add tests and cleanup logic