refactor main.py and create lib, implement map reduce