Added a close delegate for peers.

Lots of memory leak related fixes.
Some clean up.