Break code into modules

Probably introduced lots of bugs