improve time complexity