Fix crash with invalid reference.
Nicer debug output.
Textual cleanup.