Refactored to make better use of structs and interfaces