rewritten parts of the code