Added some comments, cleaned up. Hopefully no new bugs