factor out more cmmon code.