Fix token-getter code and refactor into a get_token() function.