This needs cleaning up further.