Perfomance tweaks. Bug fixes