improved performance and some other fixes