bug fixes and major refactoring to improve error logging