Adopt to new API format and fix some bugs