Refactored mode calculation.  Now uses HashMap.