Moved replacing of variables into own function