Add new iterative alignment to old code. Problem seems to be on second iteration of the pattern recognition.
See marked improvement in residual with new iterative alignment.