[TW342] remove repeated code and clean it up a bit