Restructured the code for better OO.