cleanup and improved error handling

Some code cleanup and improves a case where a thrown exception may have
been ignored.