Refactoring of code (I've really messed up before!)