Isn't this function already long enough? Can't you put this in a separate function?