full implementation of training algorithm