[refactor] Move code out of core/state.py