got rid of most of the printfs, added an error handler