Fix a few issues

including preventing segfaults, preventing addresses from aliasing, and making valgrind a little happier.