bug fix and speed up