Refactor PCH and add template state machine class