Fixed some bugs and added a bit of commenting