modified test.R and fix some bugs