More stable implementation of logistic function and its derivative by @fabianp