Main function and error catch-all