Changed code after a second review from Simon and fixed a minor bug