Refactoring in nested loops, ('-1' problem and break label)