Refactor, bugfixes