Fixed bugs and optimized a bit more