Clean codes

reuse dimensions wherever relevant. Some variables are renamed
descriptively.