Refactored code for ptamm