fix firefox bugs

And: rewrite for better jquery integration.