a version that finally produces the expected results