Optimize LD calculator further, fix bug.