code cleanup and final test implementation