Little code improvement

Here is a little code improvement:
- Some vars declarations are moved to the top, away from ```forEachLimit``` function, because they are the same on each iteration and we can specify them once.
- Checking for file existance are moved to the top of function.
- Unneeded ```extension``` variable is removed.