diff --git a/include/PacketLibDefinition.h b/include/PacketLibDefinition.h
index accbd1a1ac7a40fe01ad18236bbd2c698fa9cd2e..c802e690a4d904478c6642203a34e9cab1c80a34 100644
--- a/include/PacketLibDefinition.h
+++ b/include/PacketLibDefinition.h
@@ -36,9 +36,25 @@
 #define PACKETNOTRECOGNIZED 0
 /// define NULL 0
 
-//0 for x86
-//1 for motorola
-#define ARCH_BIGENDIAN 0
+/*
+ * Little Endian or Big Endian ?
+ * Overwrite the #define below if you know your architecture endianess
+ */
+#if defined (__GLIBC__)
+#  include <endian.h>
+#  if (__BYTE_ORDER == __BIG_ENDIAN)
+#     define ARCH_BIGENDIAN 1
+#  endif
+#elif (defined(__BIG_ENDIAN__) || defined(__BIG_ENDIAN) || defined(_BIG_ENDIAN)) && !(defined(__LITTLE_ENDIAN__) || defined(__LITTLE_ENDIAN) || defined(_LITTLE_ENDIAN))
+#  define ARCH_BIGENDIAN 1
+#elif defined(__sparc) || defined(__sparc__) \
+   || defined(__powerpc__) || defined(__ppc__) || defined(__PPC__) \
+   || defined(__hpux)  || defined(__hppa) \
+   || defined(_MIPSEB) || defined(__s390__)
+#  define ARCH_BIGENDIAN 1
+#else
+/* Little Endian assumed. PDP Endian and other very rare endian format are unsupported. */
+#endif
 
 enum CompressionAlgorithms  { NONE, LZ4 };
 
diff --git a/src/ByteStream.cpp b/src/ByteStream.cpp
index 717a9af242ad9d9cfaf35913a6c817b1039f3a90..ce95aac9fd1d135cfc281890c502fe8551b83064 100644
--- a/src/ByteStream.cpp
+++ b/src/ByteStream.cpp
@@ -423,47 +423,6 @@ bool PacketLib::ByteStream::setWord(dword start, word value)
         stream[start] = b1;
         stream[start+1] = b2;
 	}
-	
-	/*
-	if(!ARCH_BIGENDIAN && !bigendian )
-	{
-		//noswap
-        stream[start] = b1;
-        stream[start+1] = b2;
-    }
-	if(ARCH_BIGENDIAN && bigendian )
-	{
-		//noswap
-        stream[start] = b1;
-        stream[start+1] = b2;
-    }
-	if(!ARCH_BIGENDIAN && bigendian )
-	{
-        /// Swap
-        stream[start] = b2;
-        stream[start+1] = b1;
-    }
-	if(ARCH_BIGENDIAN && !bigendian )
-	{
-        /// Swap
-        stream[start] = b2;
-        stream[start+1] = b1;
-    }
-	 */
-	   /*
-    if((bigendian && !ARCH_BIGENDIAN) || (!bigendian && ARCH_BIGENDIAN))
-    {
-        /// Swap
-        stream[start] = b2;
-        stream[start+1] = b1;
-    }
-    else
-    {
-        /// No swap for x86
-        stream[start] = b1;
-        stream[start+1] = b2;
-    }
-		*/
     return true;
 
 }
diff --git a/src/Packet.cpp b/src/Packet.cpp
index afc0f1762611462b78aba6403b239c0d39950f16..1f1db9f2c3c9bc7b1e4cf8ab44b945ed53409a72 100644
--- a/src/Packet.cpp
+++ b/src/Packet.cpp
@@ -616,7 +616,11 @@ ByteStreamPtr Packet::encodeAndSetData(ByteStreamPtr sourceDataVariable)
 		if(sourceDataVariable->size() != size() - dimPacketStartingFixedPart - dimPacketTail)
 			throw new PacketException("the size of the sourceDataVariable is wrong");
 		bool swapped = false;
-		if((!ARCH_BIGENDIAN && bigendian) || (ARCH_BIGENDIAN && !bigendian)) {
+#ifdef ARCH_BIGENDIAN
+		if(!bigendian) {
+#else
+		if(bigendian) {
+#endif
 			sourceDataVariable->swapWord();
 			swapped = true;
 		}
diff --git a/src/PartOfPacket.cpp b/src/PartOfPacket.cpp
index 1756cda8457e7c2a2a66f6ac7df208cf25cd13bc..0869150a8671fbdb449e14ba72a3a06af222eec5 100644
--- a/src/PartOfPacket.cpp
+++ b/src/PartOfPacket.cpp
@@ -16,6 +16,9 @@
  ***************************************************************************/
 
 #include "PartOfPacket.h"
+#include "PacketLibDefinition.h"
+
+//#define DEBUG 1
 
 using namespace PacketLib;
 
@@ -227,7 +230,6 @@ bool PartOfPacket::decode() {
 	if(decoded)
 		return true;
 	Field* ftemp;
-    //this->stream->setStream(s, 0, s->size() - 1);
     /// The pointer is converted from byte to void. The reading from file allows the correct data interpretation
     /// for big or little endian machines
     byte* stream = (byte*) this->stream->stream;
@@ -243,53 +245,38 @@ bool PartOfPacket::decode() {
     /// number of shift for elaboration
     short numberOfShift = 0;
     /// number of fields
-    //unsigned nof = getNumberOfFields();
     word nof = numberOfFields;
     for(word i=0; i<nof; i++)
     {
         ftemp =  fields[i];
         dimbit = ftemp->size();
         /// Temporary word to be modified for the elaboration
-        byte bh = *(stream + posword);
-        byte bl = *(stream + posword + 1);
-        //word wordtemp = *(stream + posword);
         word wordtemp;
-        if (this->stream->isBigendian())
-            wordtemp = bh * 256 + bl;
-        else
-            wordtemp = bl * 256 + bh;
+		// swap bytes if the machine is bigendian and the stream is little endian and vice-versa.
+#ifdef ARCH_BIGENDIAN
+		if (!this->stream->isBigendian()) {
+			byte bless = *(stream + posword);
+			byte bmost = *(stream + posword + 1);
+			wordtemp = (bmost << 8) + bless;
+#else
+		if (this->stream->isBigendian()) {
+			byte bmost = *(stream + posword);
+			byte bless = *(stream + posword + 1);
+			wordtemp = (bmost << 8) | bless;
+#ifdef DEBUG
+			std::cout << "word: " << wordtemp << std::endl;
+#endif
+#endif
+		} else {
+            wordtemp = *( (word*)(stream + posword) );
+#ifdef DEBUG
+			std::cout << "word: " << wordtemp << std::endl;
+#endif
+		}
+
         numberOfShift = 16 - (posbit + dimbit);
-        //parte nuova
-        /// \remarks if the condition is not fulfilled, the code is equal to the versions older than PacketLib 1.3.3
-        if(numberOfShift < 0)   
-        {
-            short currentDimBit = dimbit + numberOfShift;
-            dimbit = abs(numberOfShift);
-            ftemp->value = (wordtemp & pattern[currentDimBit] ) << dimbit;
-            posbit = 0;
-            posword += 2;
-            bh = *(stream + posword);
-            bl = *(stream + posword + 1);
-            if (this->stream->isBigendian())
-                wordtemp = bh * 256 + bl;
-            else
-                wordtemp = bl * 256 + bh;
-
-            numberOfShift = 16 - (posbit + dimbit);
-            wordtemp = wordtemp >> numberOfShift;
-            /*		cout << i << ": " << ftemp->value << endl;
-            		cout << i << ": " << (ftemp->value << currentDimBit) << endl;
-            		cout << i << ": " << wordtemp << endl;*/
-            ftemp->value = ftemp->value | (wordtemp & pattern[dimbit]);
-            /*		cout << i << ": " << ftemp->value << endl;
-            		cout << i << ": " << (wordtemp & pattern[dimbit]) << endl;*/
-        }
-        else
-        {
-            //questa fa parte della parte vecchia
-            wordtemp = wordtemp >> numberOfShift;
-            ftemp->value = wordtemp & pattern[dimbit];
-        }
+        wordtemp = wordtemp >> numberOfShift;
+        ftemp->value = wordtemp & pattern[dimbit];
         /// Upgrade of pobit and posword
         posbit += dimbit;
         if(posbit >=16)
@@ -302,8 +289,6 @@ bool PartOfPacket::decode() {
     return true;
 }
 
-
-
 char** PartOfPacket::printValue(const char* addString)
 {
 	decode();
@@ -497,6 +482,9 @@ float PartOfPacket::getFieldValue_32f(word index)
         float f;	
     } u;
     u.i =  ( (dword) getFieldValue(index) << 16) | ( (dword) getFieldValue(index + 1) );
+#ifdef DEBUG
+	std::cout << "float: " << u.i << std::endl;
+#endif
     return u.f;
 }
 
@@ -515,10 +503,10 @@ double PartOfPacket::getFieldValue_64f(word index)
         double d;	
     } u;
 
-#ifdef __x86_64__
     u.i = (unsigned long) ( (unsigned long)  getFieldValue(index) << (48)) | ( (unsigned long) getFieldValue(index + 1) << (32)) | ( (unsigned long) getFieldValue(index + 2) << (16)) | ( (unsigned long) getFieldValue(index + 3) );
+#ifdef DEBUG
+	std::cout << "double: " << u.d << std::endl;
 #endif
-
     return u.d;
 }
 
@@ -572,6 +560,9 @@ signed long PartOfPacket::getFieldValue_32i(word index)
 {
     long l;
     l = (long)(getFieldValue(index) << 16) | (long)getFieldValue(index + 1);
+#ifdef DEBUG
+	std::cout << "int32: " << l << std::endl;
+#endif
     return l;
 }
 
@@ -588,6 +579,9 @@ unsigned long PartOfPacket::getFieldValue_32ui(word index)
 {
     dword l;
     l = (dword)(getFieldValue(index) << 16) | (dword)getFieldValue(index + 1);
+#ifdef DEBUG
+	std::cout << "uint32: " << l << std::endl;
+#endif
     return l;
 }