slight speed optimization and 1 bug fix