Did some refactoring and fixed a fatal typo