Remove unused code
Support case where vector input is already an output of scan
Add tests and modify the optimization to cover new cases