Rewrite some parts to be able to add tests