Fixed keychain issue for OS X and memory leaks

Fixed keychain issue for OS X and memory leaks