Some engine fixes

Fix initializing return buffer.
Fix possible array overrun.
Remove unneeded double-initialization of the vars.

This is #834 rebased onto the correct branch.