Fix a undefined ptr dereference, a couple of memory leaks and reorganize declarations
Remove Xcode stuffs :p