More efficient problem 4