Added some logging and exception handling