removed unnecessary code and refactor build_state function