Added better error management with error to stderr and emit. Also fixed bug heart beat