changed error handling and fixed bugs