more code rewrites, cleanup