Cleaned up code a bit to ensure proper deletion of objects, when no longer used.