Fixed reference bug, optimizations.