Pastable version of how to refactor tricky logic involving consecutive sets?