first step restructuring code

it got too large :)