Fixes to endif handling