optimize speed and bug fixes