Refactor map reduce and search