added variable and logic to keep track of previous word position