Did some reformatting, refactoring and corrections