Bug fix of potentially dangerous "head" call.

Code to ensure the call to head is done safely when the stack is
empty.