Refactor and cleanup code for Level 4.