Fixes incorrect handling of nested global close tag

Includes tests to verify correct functionality

See also http://forum.fok.nl/topic/2108879