remove dead code
re-architect remaining global variables