code cleanup and explained