Added tests and error logging