Small changes, incorporating Amir's code