optimize code to speedup