added latest code from internal repository. New features added, check the NEWS file for details