Added logging, refactored closing streams and url connection