Fixed task state serialization.

Rather than serializing several messages separately, serialize one
message per app to/from the state handler.
Fixed a bug in fetching task state.