Added lots of commenting and fixed a few issues