Beautified (j in loops to i, replaced functions)