Added checks of return value of malloc function