Forked up some optimizations.