Refactor and flesh out the decoding loop