Added lifetime management code. Very little testing has been done, but this might be what is needed.