Refactor and simplify here and there. Minor bugfixes as well.