Restructure; several fixes and improvements