Various performance and bug fixes