Added error tolerance and logging. Slight tweaks.