merged handleText code