Performance improvement

I fixed the bug you mentioned just now. Here is the new pull request.