Code cleanup and Unit Tests