Minor error correction and refactoring