Default How can I restore and Reroot

I am on MJ5 and would like to completely restore my phone back to factory and Reroot. I notice little bugs here. so any info would be great. Thanks