c version, work in progress