refactored expansion of variables