Made nested template regex less flexible; fixed bugs