Cleaned up / documented core implementation