Add error handling. Add tests. Code refactoring