restructuring some code