Some optimizations and improvements