re-writing much more simply now