Refacotring the code

rename the argument,rewrite the nested loops