Loop optimizations.

Several loops which works with tables didn't have good performance