Made treatment of NA values much more elegant using which()