Reorganize things and add doc strings to clarify whats going on here.