This needs more cleaning up...