Moved similar code to a generalised method