modification of Robert's code.