This is a big commit, but a necessary one, as too many things were intertwined. As a result, the code is now cleaner.