working version, still needs some config.state adjustments