rearrange code slightly. add simple 'test' script