Improved code for dealing with endstops.