Updated with newly efficient solution for problem 5