added api docs
fixed error message and clean up code