rebase #39  patch and merge it.

rebase @smilledge  's #39 patch. and merge it to upstream.