port from http://svn.coderepos.org/share/docs/amachang/20080813-procamp2008/index.html