Reuse pre allocated memory.
Add check and better error message
Partial fix