Added check for big/little endian. Cleaned up code