Refactor HTML handling