Bug fixes & refactoring