Added some exception handling and logging