Modularized and beautified major parts of code.