Added some cleanup code to prevent memory leaks in custom directives.