Refactored element changing function

Intentional removal of duplication