Refactoring to use consistent loops

global variable `j`