Loop optimizations.

Several loops which works with tables didn't have good performance
Merge pull request #1012 from najor/hotfix-table-loop-for-optimization

Loop optimizations.