added docs, many tidy-ups