error checking + refactotoring