bug fix - algorithm is returning correct result now
performance improvement