Fix #4267

Squashed commit of the following:

commit bdb98355e8f7123b5021a855b79e87e820ac3c30
Author: José Raddaoui Marín <raddaouimarin@gmail.com>
Date:   Wed Feb 27 02:40:59 2013 +0100

    Move misplaced endif