Re-add the line straightening algo, from wraitii's latest commit