refine logic

move iteration out of the main method