a first attempt at restructuring the code