Some rework, cleanup and commenting