factor some stuff, and make them cleaner
use Data.Set for faster query than list.