factor out reusable code