merged with GP's code