Checked for bugs and fixed the few I found