refactor global stuff to ctx