merged with final version of Remco's code