[TIMOB-13775] Fixed some small bugs and cleaned up some of the logic.