Fix headers and add a small NPE check