Mostly restructure of code