Started restructuring code to make it more manageable