4th version of algorithm