Added error handling. Extended unit tests.