Added some exception handling and error logging