Moved processing out of main, reorganized some stuff