Loss function fixed; some speed improvement