add test_orders.
fix bug exposed by test,
and do a bit of unrelated cleanup.