move sequence handling code around