Basic error cases handling

Error cases handling is basically improved to especially handle more
NULLPTR cases according to comments fro Max.