Changed more globals to args
Reorganized code to expose additional parallelism