Alarm handling, code cleanup

Add code to handle alarms (currently only an emergency stop can trigger
an alarm). Also clean up some variable definitions.