I think that function could really benefit from comments :)