added more logging and error handling