Based on Erick Demaine's course