Adding error checking
merged