Refactoring code
Tested generate token function

Not very happy with the way it is done...