Refactoring, commenting and organization

The code was working, now it's pretty and easy to read