added code to save & process training trace, enhanced randomness