refactor sequence code