more flexible version, with learning