refactor to use for each resources

refactor the code to use for each resources