I think you should factor out `isFocused(window)` function out of this.