Major bug fixes and addition of better error handling and logging.