major bug fix + optimization