added token generation and fixed some bugs