fix some performance issue.
remove some duplicating codes.