Having tested the output I now think we should not use the word NULL here.... I think we should not output anything for the keytype if it is not set, so please change NULL in this line to an empty string.