Restructuring some code