Added basic error handling and exceptions