2. Add tests and minor refactoring