new (and working) version transformed to jQuery plugin