Improved event handling (IE8) and lots of code cleanup