Further code cleanup, restructuring and clarification.w!