Don't modify scan during loop closure check - work on a copy of it
Protect functions that throw away const-ness (check dirty flag) with mutex
Fix newlines (dos2unix) & superfluous whitespace
Fix cppcheck warnings