Restructure and bug fixing