Code reorganization. (still buggy)