condensed code and put nongui merge into its own function