fix bug and fix error logging;