Cleanup code and added test code