Rewrote and fixed bin2struct algorithm

It should now be faster and more reliable.