some performance and bug fixes