restructure some code