Fix memory and agent misbehaviour

Change-Id: I1a28de0a91332418154c33fdf7f037e0695ba224