complete new scheme replacing applydefine, untested