Clean up code, fix errors in parts 2 and 4