optimization and bug fix