Refactoring: extract weird part in every loop.