Codes optimizing : merging, removing redundant loop