chore: reorder functions

Reorder functions and fix variable access