ported more JS code to CS