Refactoring & bug fixes