improve example code and fix some bugs