Fix and refactor sg plugin
Fix sg template
Fix sgAncestors directive