Updated code to match Denis's.