write API reference. fix several bugs