refactored a few bugs and fixed a typo