Code parts from reading a book by A Troelsen