new version, minor optimizations.