Refactored token generation