Refactoring the sample token function. There is currently a bug I don't understand