Refactor Principal code into Worker and Manager