refined code--phase 1