Used TextNodes to protect against XSS

Also encapsulated the logic.