Improved performance by reducing the number of mysql queries.