Refactor mapReduce, try/catch, functions