The main training loop