Fixing out-of-bounds bugs

This commit includes some dirty fixes for bugs causing out-of-bounds indexing which can crash the fuzzer. I suspect that at least some of these bugs are rooted deeper in the code so feel free to reject the request in order to have more robust fixes.