Made for efficient solution to problem 3.