Added real detection of state instead of just an assumption about it being inbound always - oops :-D