more compact traning function