rebased with fixes from russ