Fix issues in line optimization.

Closes #266.