Better loop implementation