refactored to make it neater