Speed improvements (reduced the number of times I called window.refresh())