Better code organization

Added some comments, indentations, and moved some stuff around