Moved cachedCdf and added optimization