refactor, error checking'