removed global reference, added free when malloc, removed debug NSLOGs