Note: Only the main() was drastically changed