added training mode and fixed up code a bit