fixed possible segfault if mem allocation failed
+major change