Serious code cleanup