Fixes to variable handling, fixed order of operations