More protection against incomplete data

Also added more debug output.