pass correct referenced variables to function