Fixed unsafe unaligned memory access that was probably causing bug
triggered by ARM.
Code refactored to increase reuse.