Add tests; code refactoring