re-ordered a bit and added more comments.

also converted all tabs to spaces so that it reads better