Some refactoring in preparation for extracting state