refactoring for syntax upgrade, fixed some bugs and perf. issues