Fixed previous term algorithm
Previous term algorithm fixed