correct algorithm. now returns correct results