properly handle undefined as previous implementation according to Wikipedia does NOT work