Added comments, new functions, hopefully some modularity to the code