Made loss function much faster