some modifications and optimizations