Cleaned up code and added comments to indicate where certain bugs were fixed.