Added error checking - fixed #4