Revised code for Ricardo