Add tests and improve getting a token