minor reorganization
make a bunch of functions pure