Corrected bugs found during tests

Change-Id: Ia0ebb9bcc54dd4540d5d1a00675659ce6bb9a13e