Extracted some code into separate classes for better re-usability.