reorganize API wrapper code and add some more documentation / comments