Improve loop logic.