Updated with semi-working code
See the comments at the end of the code for an explanation
but basically, it runs and wraps round etc. as it should or seems to. At
one point though we get a bizarre packet with type 0 that doesn't really
fit and I'm not sure why yet. Feel free to run the code and see what you
think or point out any obvious (or otherwise) mistakes