dubious if this is correct but seems to work ish