move some code around to organize better