Added checking for malloc fails