refactoring some ugly stuff