Refactor 006.hs and optimize 007.hs