Refactor into modules and reorganize file structure