Re-structured template code and fixed bug in registration