* Refactored to use TreeSet instead of naive implementation