Added error logging and recovery