Add state saving/loading, there is some ugly code though