working implementation with greedy linesplitting