removed some dependence on global variables