Fixed encoding issue
Added error log
Code cleanup