fixing some bugs, improving performance by 10%