refactor some element creation code