Fix #3693 (variable ref'ed before assignment)

Also, fix another reference before assignment in the "test=True" code
block
Fix stack trace introduced with 7c46123