Fix bug in scan merge optimization reported by Yao Li.
Merge pull request #1506 from lamblin/merge_scan

Fix bug in scan merge optimization reported by Yao Li.