added some exception handling and logging