Changed perplexity function