Performance improvement

I fixed the bug you mentioned just now. Here is the new pull request and the improvement still work.