refactored and sanitised