Added checks of return valuse of malloc function