implemented parts of training algorithm in C, updated ldm function