Refactor, almost completely working again.