move everything into the sub function