rename some variables, move checks to separate function