Fix issue where variable is used before it is declared

Fixes #3083