Improved error message logging,fixed code issues