deleted pos_old and char_len. reduced nested if statements.