Fix the bug introdiced in PR #866, where the $format was never set.

This fixes the bug introduced in PR #866.
can we move the assignment to before the if?