Overhauled the helper templates.

Supports const/immutable/shared.
Fixed some bugs.