WIP: move code to modules, rewrite it