removed debug prints, added error checking