Added a unit test and a minor fix to help garbage collection.