Improved error checking
minor edits