trial and error but seems to work

supercedes #8 and #7