Refactoring token writing