refactored to make code simpler