code refactoring
fixed wrong index increment