added more error handling and took out debug printing