Added basic error handling. Still needs some work, but it works for now!