Code cleanup

Error conditions were not being handled correctly