Close resources before return

Cleans up some InputStreams that were left open
Adds a null check