[TIMOB-13697] Some code cleanup and bug fixes.