Improved memory handling/cleanup
Keep track of object counts to avoid linear search