Fixed bug where variables referenced where not reachable (out of scope)

Change-Id: I9a9f9c810a9a5da1d169c2e7d04cc2c5a59a88d7