Restructuring and major fixes