added deletion of allocated pointers as suggested by Marcel Lüthi