Fix possible memory overflow and initialize memory

Per @mattn