fix time complexity