Add max_gap kwarg to logical_intervals and refactor to use state_intervals