Bunch of changes and optimizations