factor out helper code