move next trial logic into its own function