Rewrite as jQuery plugin