Added a better loop implementation.