Tidied the code

Tidying up the code while searching for the bug.