Modified to make more resistant to bad data.