Code reorganization and added comments.