Fixing bad_alloc exception