moved some invariant code out of a loop.

- also made it use numpy min/max instead of standard min/max
- factored out the code to build the window from coo_matrices so that
  it can be profiled.
added some extra instrumentation, maybe too heavy.