Undefined variable fix

This is a separate PR as suggested in #245, to fix an undefined variable.
Please review the change as I'm not 100% sure what was the intended behaviour.