Implement the algorithm for moving cues to their best position.

Builds on pull request #239.

Fixes #112.
Fixes #203.