more efficient solution to ex 7