Fixed various state related bugs