adding format_state function and spec tests