Refactoring for sam and rest