Some serious code cleanup