Move main stuff into a proper main function