Complete refactor

+ Split into module
+ Write proper functions for everything
+ More work to do, of course