replace handrolled for loop with TAILQ_FOREACH; from andres.p@zoho.com