Optimise retain_variable_keys function.
Move check outside of loop.
codestyle fixes
Merge pull request #1624 from dannyvankooten/master

Optimise retain_variable_keys function. props @dannyvankooten