Bug fixes and backup before refactoring