refactoring some helpers into a new module and fixing some unicode-related bugs