Shortened code for Prob 8