cleaned up error checking a bit
adding spec tests