Fixed 4 bugs and added some explaining comments