Improve performance of loops. Precalculate limit.