found more efficient solution to problem 1