Added extra logging and error handling