added lots of documentation, separated R and C versions. need to write more documentation