refactor for readability and modularization. Also more work on token
submission.