optimal action code. not yet tested