Added distinct query mode; more modularized organization