none of the following has changed except for indenting (due to the added try-except)