Improved the efficiency of collectValidLocations().

Added tests for collectValidLocations().