Error checks and refactorings