Add type field.
Total rewrite of train.py