c-implementation (not working, just for reference)