Extensive code cleanup