new logic. ealier technique wasn't the best soln. Now using array as intermediate