fork from http://www.creytiv.com/rem.html and do some modifications