fixed error checking, renamed functions