Fixed scanning in the algorithm which still had some small bugs.