implemented and (mostly) tested the originally target function. Ready to merge with gochem