Minor bugfixes and code optimization

I tested this in IE 6-8 (native), IE6-10 (compatibility mode), Firefox 2.0, Opera 9.52 and everything seems to work
This is a nice optimization! :)