Next 10 lines are hardly readable. should be refactored