fixed reverse application of algorithm