Restructure and bugfix