Updated to support arbitrary templates