refined code--phase 2