Trivial style comment: I'd add a space before the "then".