More code cleanup and added unit tests