public static function writeUnsignedVarInt($data) {
        if($data < 0) {
            throw new InvalidDataException('Cannot write negative values');
        }
        $orig = $data;

        //single bytes don't need encoding
        if ($data < 0x80) {
            return new VarInt($data, pack('C', $data), 1);
        }

        $encodedBytes = [];
        while ($data > 0) {
            $encodedBytes[] = 0x80 | ($data & 0x7f);
            $data >>= 7;
        }

        //remove most sig bit from final value
        $encodedBytes[count($encodedBytes)-1] &= 0x7f;

        //build the actual bytes from the encoded array
        $bytes = call_user_func_array('pack', array_merge(array('C*'), $encodedBytes));;

        return new VarInt($orig, $bytes, strlen($bytes));
    }