Extracted the core part into a separate file.