part 2 actually worked without optimizations, but i almost made out of ram so i made the optimizations