optimized NNP function