Add support for #3483

Also refactored state.py a bit and made some changes a long the way to support pydsl, plus fixed a bug in grains/__init__.py.