fixed reverse iteration logic