I think we can rewrite this function better to have just one return statement that is guaranteed to execute (perhaps in a try-except-else-finally clause).