Add getstate & rearrange code