Refactor scope handling and replaceVariables()

Now replaceVariables does not take variables as a parameter
but always uses the current scope.

This also fixes updating variables from outside current scope.

Change-Id: I6bb30bde42991b2eebe30f1406bac3afc9807125