Further code cleanup and corrections