speedup and fixing compatibility issues
Merge pull request #2 from dac922/master

more optimization