Updated the code to include a master election