Start to implement issue #27.

May need more testing and tuning.