Version using hashsets and sequences, rather than lists