Refactor, write documentation