improved version…soon to be abandoned in favour of a python based one