Fix for both issue #1 and #2

I have made fix for both #1 and #2 . This fix working fine on both firefox 31.0 and Chromium 36.0.1985. Please verify. 

Thank you so much for rewriting the code :+1:  . It helped me alot to make a jQuery version of this called kooy for our website [vanmaram.com](http://www.vanmaram.com)