refined code to improve performance.