fix bugs / refactor a bit