More comments and documentation!

Also some refactoring with the order of the subroutines