Merged two loops in one