Remove a big chunk of duplicate code by re-ordering.

 * Remove the refilter code that was in there twice (and directly squash a small bug)
 * Pull out the window position calculation in a sub-function.