First working multi-threaded version; R API is not re-entrant; this was causing crashes