refactor
fix for last character not getting considered