more optimized than before, still exceeding time limit though