Added training and fixed a couple of bugs