a lot Max's optimizations