dynamic memory allocation in get_token

changed functions to return token instead of token pointer.

added dynamic memory allocation

other minor fixes