remove useless clone
small code refactoring
code simplification
Add assert, remove useless check and move variable closer to where they are used.
move costly check later
Make cheap comparison earlier. This make an scan opt from 4s to 1.Xs.