Rewrote Deferred selection code to not use "window".

This means it can be used in a web worker.