optimize solution to #4