Refactored slightly
Added error handling and output to stderr
Set application name