some changes to improve performance and allow mysql query caching to have some effect