Algorithm to split lines that doesn't work