removed debug statements and added some error checking