Some small bug fixes and optimizations.