Bug Fixes and Refactoring