Added more comments to the code

It's probably more understandable now.