further improved readibility of code.

Added more comments and rearracnged code a bit.