REV: refactor to change all possible loop to use functional style