Hi, first of all, i like to see this beeing implemented :-)

But i think the Chmod should be done after the group detection, as it is not yet known if there is a group to give the access to, at this point.