changed: Refactor to remove code duplication etc.