Refactor main stuff to methods