vectorized slow loops and fixed some issues.