Pulled out endian conversion so it could be reused