Cleaned up and commented some parts of the code