Refactored some stuff as per code review.