Revised code and made minor refactorings