Fixed train and added lots of tests.

Fixes #25, fixes #24, fixes #10.