Added separate token functions and refactored code