Fix bug which occurs when window is resized

I have took into account all your suggestions and refactored everything into an even cleaner pull request. If there's anything else you'd like me to do, let me know!