refactor psm code