Ported to C, added several features