convert this code to a more efficient idiom