Fix some build elements

Could there be a race condition? Perhaps it would be better to return a structure that later the caller would have to pass back to restore_privileges...