Major refactoration. Close #1. Close #2. Close #3. Close #4.