Add tests for more robust utility functions, juggle lines, remove old functions, factor out confusing logic