Fixed mode calculation so that it doesn't crash for pathological pixel distributions.