Make element generation idempotent

Make element generation idempotent (fix double rendering issue) and code
cleanup