fix function creation in loop