refactor
use deques instead of sets