better handling of exceptions, eliminated infinite loop on error