updated implementation with hashset