Refactoring some ugly code.