Refactor text manipulation