Small optimization for problem 1