Giant planet formation models with a self-consistent treatment of the heavy elements