Refactored pinning to be more compact and testable