refactored to be a bit more structured