Update cap.c

Modified internals for sanity sakes.  Additional functionality.
See also the cap repository on github.