Fix unnecessary array copying. Add error messages for exceptional cases. Don't assert. Not the same as Eleriseth's patch, but thanks Eleriseth for pointing out the issue.