working algorithm -- was missing lambda