Cleanup

Various cleanup and memory handling fixes based on our use of janus.

Please let me know if you have any comments.