This code is extracted from GAIA project.