Shared main function to four another functions. Added correct error messages with correct exit codes.