Rewrite dragging code so it will be easier to adapt in both directions.
Part of issue #36.