refer to http://amix.dk/blog/post/19486 and add some useful features