Huge code cleanup, better practices