Minor optimization to Problem 8