Slightly reworked code to be more portable (wrappable by system code); added Cocoa wrappers for OS X