move some code around for better readability