Closed #3048: refactoring codes for performance