Fix coding convention
Change to 4-arguments API, and unnest internal functions.
Update documentation