Attach resize event with addEventListener

Assigning listeners to `window.onresize` overwrites any previously attached listeners. 
Using addEventListener to attach events to avoid overwriting.

Also has a fix for variable x added to global scope by declaring x.