fixed OS X compatibility, should work also on iOS.  Added arg type checks and conversions.