solved problem 23, need to improve performance of algorithm