Code has been reviewed, not tested.