fix recursive method and add handling for nonexistent characters