refactor and check for more errors