Restructured classes and many minor code fixes