Fixed typos, added some additional comments and added a missed error check.
Added example in C.