Added further error handling - needs re-work.