Fixing endif on the incorrect position.

It was tested. The problem was a early close from a if loop.