Refactor #260 a bit to optimize some code.