Adjusted Anthony's code