Some Small Bugs and Refactoring