improved speed and bug fix