refactored dom manipulation code