Fixed bad variable passing to functions. Used lambda instead.