refactored out some ugly stuff