some minor corrections and refactorization