Cleaned up some of the examples and put in place some pretection against memory leaks where possible