A bit more refactoring done so this doesn't look like a high-school project