* remove console.log statement on error instead emit error event
* move var declarations to top of function